#include "interface.h"
#include "Analyzer.h"
#include "load-graph.h"

extern Analyzer * an;
/*Interface::Interface(Analyzer * _an)
{
this->an = _an;
this->an->gui = this;
}*/

 gboolean time_handler(gpointer data)
 {
	 Interface * it = static_cast<Interface *> (data);
	 gtk_text_buffer_set_text(it->txtbuffer,it->tmptxt,-1);
	 return true;
 }


 static void toggleListeningCallback(GtkWidget *widget, gpointer data)
 {

	 Interface * it = static_cast<Interface *> (data);
	 it->toggleListening();
 }


 void Interface::toggleListening()
 {

	 if ( an->keepRunning == false )
	 	{
		 gtk_spin_button_update(GTK_SPIN_BUTTON(spinbutton));
		 int spinvalue = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinbutton));
		 sprintf(tmptxt,"Listening on port: %i\n",spinvalue);
		 printBold(tmptxt);
		 an->setPort(spinvalue);

	 		an->startListening();
	 		gtk_widget_set_sensitive (GTK_WIDGET (spinbutton), false);
	 		gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button),GTK_STOCK_MEDIA_PAUSE);
	 		gtk_statusbar_push(GTK_STATUSBAR(statusbar),2,"Listening...");
	 	}else{
	 		printBold("Stop\n");
	 		gtk_widget_set_sensitive (GTK_WIDGET (spinbutton), true);
	 		gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button),GTK_STOCK_MEDIA_PLAY);
	 		gtk_statusbar_push(GTK_STATUSBAR(statusbar),1,"Pause");
	 		an->stopListening();
	 	}

 }

 void Interface::initGui(int argc, char *argv[])
 {
 	gtk_init(&argc, &argv);

 	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 	gtk_window_set_title(GTK_WINDOW(window), "TS Mpeg2 Analyzer... ");
 	gtk_window_set_default_size(GTK_WINDOW(window), 800, 500);
 	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

 	vbox = gtk_vbox_new(FALSE, 0);
     gtk_container_add(GTK_CONTAINER(window), vbox);

     table = gtk_table_new(2, 2, TRUE);
      gtk_table_set_row_spacings(GTK_TABLE(table), 10);
      gtk_table_set_col_spacings(GTK_TABLE(table), 10);
      gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 1);

      statusbar = gtk_statusbar_new();
      gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(statusbar),false);
      gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, TRUE, 1);


      vbox2 = gtk_vbox_new(FALSE,1);
      frame1 = gtk_frame_new("Error Graph");
      gtk_frame_set_shadow_type(GTK_FRAME(frame1), GTK_SHADOW_ETCHED_IN);
      frame2 = gtk_frame_new("Jitter Graph");
      gtk_frame_set_shadow_type(GTK_FRAME(frame2), GTK_SHADOW_ETCHED_IN);
      frame3 = gtk_frame_new("Bitrate Graph");
      gtk_frame_set_shadow_type(GTK_FRAME(frame3), GTK_SHADOW_ETCHED_IN);


      gtk_table_attach_defaults(GTK_TABLE(table), vbox2, 0, 1, 0, 1);
      gtk_table_attach_defaults(GTK_TABLE(table), frame1, 0, 1, 1, 2);
      gtk_table_attach_defaults(GTK_TABLE(table), frame2, 1, 2, 0, 1);
      gtk_table_attach_defaults(GTK_TABLE(table), frame3, 1, 2, 1, 2);


      jitterGraph = new LoadGraph(2);
      errorGraph = new LoadGraph(1);
      bitrateGraph = new LoadGraph(0);
      gtk_container_add(GTK_CONTAINER(frame1),load_graph_get_widget(errorGraph));
      gtk_container_add(GTK_CONTAINER(frame2),load_graph_get_widget(jitterGraph));
      gtk_container_add(GTK_CONTAINER(frame3),load_graph_get_widget(bitrateGraph));

      load_graph_start(jitterGraph);
      load_graph_start(errorGraph);
      load_graph_start(bitrateGraph);


     hbox = gtk_hbox_new(FALSE,1);
     scrolled_window = gtk_scrolled_window_new(NULL,NULL);

     txtview = gtk_text_view_new();
     txtbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtview));

 	gtk_text_buffer_create_tag(txtbuffer, "gap",
 			"pixels_above_lines", 30, NULL);
 	gtk_text_buffer_create_tag(txtbuffer, "lmarg",
 			"left_margin", 5, NULL);
 	gtk_text_buffer_create_tag(txtbuffer, "blue_fg",
 			"foreground", "blue", NULL);
 	gtk_text_buffer_create_tag(txtbuffer, "red_fg",
 			"foreground", "red", NULL);
 	gtk_text_buffer_create_tag(txtbuffer, "gray_bg",
 			"background", "gray", NULL);
 	gtk_text_buffer_create_tag(txtbuffer, "italic",
 			"style", PANGO_STYLE_ITALIC, NULL);
 	gtk_text_buffer_create_tag(txtbuffer, "bold",
 			"weight", PANGO_WEIGHT_BOLD, NULL);





     gtk_box_pack_start(GTK_BOX(vbox2), hbox, FALSE, TRUE, 1);
     gtk_box_pack_start(GTK_BOX(vbox2), scrolled_window, TRUE, TRUE, 0);
     gtk_scrolled_window_add_with_viewport((GtkScrolledWindow *)scrolled_window,txtview);


     label = gtk_label_new("Port:");
     spinbutton = gtk_spin_button_new_with_range(0,60000,1);
     button  = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_PLAY);
     gtk_box_pack_start(GTK_BOX(hbox),label, FALSE, TRUE, 1);
     gtk_box_pack_start(GTK_BOX(hbox),spinbutton, FALSE, TRUE, 1);
     gtk_box_pack_start(GTK_BOX(hbox),(GtkWidget *) button, FALSE, TRUE, 1);
     gtk_text_buffer_get_iter_at_offset(txtbuffer, &iter, 0);



    g_timeout_add(1000,time_handler,this);

 	g_signal_connect(button, "clicked", G_CALLBACK(toggleListeningCallback), this);
    g_signal_connect_swapped(G_OBJECT(window), "destroy",G_CALLBACK(gtk_main_quit), G_OBJECT(window));

 	gtk_widget_show_all(window);
 	gtk_main();

 }

 void Interface::cleantxt()
 {
	 strcpy(tmptxt,"");
 }

 void Interface::printBold(const gchar *text)
 {
     gtk_text_buffer_insert_with_tags_by_name (txtbuffer, &iter,text, -1, "bold", "lmarg",  NULL);
     GtkAdjustment * adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolled_window));
     gtk_adjustment_set_value(adj,gtk_adjustment_get_upper(adj));
 }

 void Interface::print(gchar *text)
 {

	 if (strlen(tmptxt) < 500)
	 {
		 strcat(tmptxt,text);
	 }else{
	 strcpy(tmptxt,&(tmptxt[strlen(text)]));
	 strcat(tmptxt,text);

	 }
	 //	 gtk_text_buffer_get_iter_at_offset(txtbuffer,&iter,0);
	 //gtk_text_buffer_set_text(txtbuffer,text,strlen(text));
//	 gtk_text_buffer_insert(GTK_TEXT_BUFFER(txtbuffer), &iter, text, -1);

 }

 void Interface::printBlue(const gchar *text)
 {
	 gtk_text_buffer_insert_with_tags_by_name(txtbuffer, &iter,text, -1, "blue_fg", "lmarg",  NULL);

 }
 void Interface::printRed(const gchar *text)
 {
	 gtk_text_buffer_insert_with_tags_by_name(txtbuffer, &iter,text, -1, "red_fg", "lmarg",  NULL);
 }

 void Interface::printItalic(const gchar *text)
 {
	 gtk_text_buffer_insert_with_tags_by_name (txtbuffer, &iter,text, -1, "italic", "lmarg",  NULL);
 }

 /*
  *     gtk_text_buffer_get_iter_at_offset(txtbuffer, &iter, 0);



       gtk_text_buffer_insert_with_tags_by_name (txtbuffer, &iter,
             "Text with colored background\n", -1, "lmarg", "gray_bg", NULL);



       gtk_text_buffer_insert_with_tags_by_name (txtbuffer, &iter,
             "Bold text\n", -1, "bold", "lmarg",  NULL);
  * */

